﻿using System;
using System.Collections.Generic;
using System.Text;

namespace AMS.DataAccess.Sql
{
    /// <summary>
    /// 构造IN的SQL表达式。形如：
    /// Name IN ('xiaoming', 'xm', 'abc')
    /// </summary>
    public class SqlConditionList : SqlExpression
    {
        /// <summary>
        /// 构造IN的SQL表达式
        /// </summary>
        protected SqlConditionList()
        {
        }

        /// <summary>
        /// 构造IN的SQL表达式
        /// </summary>
        /// <param name="column">列名</param>
        /// <param name="valueList">候选值列表</param>
        public SqlConditionList(string column, ICollection<int> valueList)
        {
            BuildContent(column, SqlStringUtils.IntListToString(valueList));
        }

        /// <summary>
        /// 构造IN的SQL表达式
        /// </summary>
        /// <param name="column">列名</param>
        /// <param name="valueList">候选值列表</param>
        public SqlConditionList(string column, ICollection<string> valueList)
        {
            BuildContent(column, SqlStringUtils.StringListToString(valueList));
        }

        /// <summary>
        /// 构造IN的SQL表达式
        /// </summary>
        /// <param name="column">列名</param>
        /// <param name="valueList">候选值列表</param>
        public SqlConditionList(string column, ICollection<DateTime> valueList, DateTimeType type)
        {
            BuildContent(column, SqlStringUtils.DateTomeListToString(valueList, type));
        }

        /// <summary>
        /// 构造表达式
        /// </summary>
        /// <param name="column">列名</param>
        /// <param name="value">候选值列表</param>
        protected void BuildContent(string column, string value)
        {
            Content.Append(column);
            Content.Append(" IN (");
            Content.Append(value);
            Content.Append(")");
        }

        /// <summary>
        /// 将内容转换成SQL语句
        /// </summary>
        /// <returns></returns>
        public override string ToString()
        {
            return Content.ToString();
        }
    }
}
